Imports System.Windows.Forms

' Handles operations for the configuration window.
Partial Public Class ConfigurationWindow
    Inherits System.Windows.Window
    Public Sub New()
        ' Init generated code.
        InitializeComponent()

        ' Fill in text boxes with settings information.
        Me.textBoxCacheFolder.Text = SettingsWrapper.CacheFolder
        Me.textBoxShareFolder.Text = SettingsWrapper.ShareFolder
        Me.textBoxPictureSize.Text = SettingsWrapper.MaxPictureSize.ToString()
        Me.textBoxP2PAddress.Text = SettingsWrapper.P2PAddress
        Me.textBoxP2PPassword.Text = SettingsWrapper.P2PPassword
    End Sub

    ' Called when user clicks the buttonSelectCache Button (see XAML)
    Private Sub OnSelectCacheFolder(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Const action As String = "Select the folder to store incoming pictures"

        Dim fbd As FolderBrowserDialog = New FolderBrowserDialog()
        fbd.Description = action

        ' Present folder browser window to user and wait for result.
        Dim result As DialogResult = fbd.ShowDialog()

        If result = System.Windows.Forms.DialogResult.OK Then
            ' Update text box.
            Me.textBoxCacheFolder.Text = fbd.SelectedPath
        End If
    End Sub

    ' Called when user clicks the buttonSelectShare Button (see XAML)
    Private Sub OnSelectShareFolder(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Const action As String = "Select the folder containing pictures to share"

        Dim fbd As FolderBrowserDialog = New FolderBrowserDialog()
        fbd.Description = action

        ' Present folder browser window to user and wait for result.
        Dim result As DialogResult = fbd.ShowDialog()

        If result = System.Windows.Forms.DialogResult.OK Then
            ' Update text box.
            Me.textBoxShareFolder.Text = fbd.SelectedPath
        End If
    End Sub

    ' Called when user clicks the buttonSaveSettings Button (see XAML)
    Private Sub OnSaveSettings(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' Note: we only save if the new values have changed.

        If Me.textBoxCacheFolder.Text <> SettingsWrapper.CacheFolder Then
            SettingsWrapper.CacheFolder = Me.textBoxCacheFolder.Text
        End If

        If Me.textBoxShareFolder.Text <> SettingsWrapper.ShareFolder Then
            SettingsWrapper.ShareFolder = Me.textBoxShareFolder.Text
        End If

        If Me.textBoxPictureSize.Text <> SettingsWrapper.MaxPictureSize.ToString() Then
            SettingsWrapper.MaxPictureSize = UInt32.Parse(Me.textBoxPictureSize.Text)
        End If

        If Me.textBoxP2PAddress.Text <> SettingsWrapper.P2PAddress Then
            SettingsWrapper.P2PAddress = Me.textBoxP2PAddress.Text
        End If

        If Me.textBoxP2PPassword.Text <> SettingsWrapper.P2PPassword Then
            SettingsWrapper.P2PPassword = Me.textBoxP2PPassword.Text
        End If
    End Sub
End Class
